var rowpolldata=1;
function addformpoll()
{
   $("#addform").append('<div id="feature'+rowdiv+'" class="itPlg addPollPlug featureclass">'+dataform['poll']+"</div>");
   var rownow=rowdiv;


  
  

   
   $('#feature'+rownow+' .deleterowbutton').click(function(){
   
    $('#feature'+rownow).remove();
    
   });
   
   
    $('#feature'+rownow+' input[name="choicebtn"]').click(function(){
   

     
     addrowdata(rownow);
    
   });
  
  
 
  
 
  
  
  
  
  
  
  
  
  
  
  
  //$('#feature'+rownow+' .group')
   
   
  
   

  
  
   rowdiv++;
}
function addrowdata(rownow,pollfid,text)
{
     var pollfidset='';
      if(pollfid)
      {
      pollfidset=pollfid;
      
      }
      
      
      var textset='';
      if(text)
      {
      textset=text;
      
      }

     var code = '<div class="choiQust" id="choiQust-'+rowpolldata+'">\n';
     code += '<input type="radio" disabled="">\n';
     code += '<input type="text" value="'+textset+'" name="labeltxt" class="clBg5 clBd2 fs03 clTxt2" kl_virtual_keyboard_secure_input="on">\n';
     
     code += '<input type="hidden" name="pollfid" value="'+pollfidset+'" >';
     
     
     code += '<input type="hidden" name="rowsetnow" value="'+rownow+'" >';
     
     code += '<input type="button" onclick="deletechoicebtn('+rowpolldata+')" value="X" class="btnGen btnRmv clTxtW cutTxt clBgBt1 fs04">\n';
     code += '</div>';
     
     
     
     $('#feature'+rownow+' #elechoice').append(code);
     
     rowpolldata++;


}
function saveformpoll(id,plugid)
{

        var obj2=new Array();
        var krow=0;
       $("#"+id+' .choiQust' ).each(function( index ) {
       //console.log( index + ": " + $( this ).text() );
       
           obj2[krow]={typeid:1,pollfid:$(this).find('input[name="pollfid"]').val(),value:$(this).find('input[name="labeltxt"]').val()};
           krow++;
       
       });
       
       var obj3=new Array();
       var krow=0;
       $("#"+id+' input[name="deletepollfid"]' ).each(function( index ) {
       //console.log( index + ": " + $( this ).text() );
       
           obj3[krow]={pollfid:$(this).val()};
           krow++;
       
       });
       var otherdata=0;
       if($("#"+id+' input[name="otherpoll"]:checked').val())
       {
       otherdata=1;
       
       }
       
       
        var obj={plugid:plugid,title:$("#"+id+' input[name="questitle"]').val(),other:otherdata,userpoll:$("#"+id+' input[name="userpoll"]:checked').val(),spollid:$("#"+id+' input[name="spollid"]').val(),datarowobj:obj2,deleterowobj:obj3};
        
   //     console.log(obj);
    //    alert('');
        
       
        return obj;
}
function getformpoll(dataplugin)
{
//console.log('1');
var arraydata=dataplugin;


var code  = '<div class="ldIt wrpFrmLd clBg4" id="pollfidset-'+arraydata['spollid']+'"><div id="pollvoteset-'+arraydata['spollid']+'" class="wrpHdInfoGen">';
code += '<h2 class="clTxt2 fs04 boldGen">Poll</h2>';
code += '<div class="wrpInFrmLd">';
code += '<h2 class="boldGen clTxt3 fs04">'+arraydata['title']+'</h2>';
code += '<div class="wrpPollInLd">';



if(arraydata['form'].length)
{
$.each(arraydata['form'], function(key, valueform) {

code += '<div class="itFrmLd radioPollLd clearfix">';
code += '<div class="pollLst clearfix">';
code += '<input type="radio" name="pollfid" value="'+valueform['pollfid']+'" id="rdoOpt3">';
code += '<label for="rdoOpt3" class="clTxt3 fs03">'+valueform['value']+'</label>';
code += '<span></span>';
code += '</div>';
code += '</div>';
})
}


if(arraydata['other']=='1')
{
code += '<div class="itFrmLd radioPollLd clearfix">';
code += '<div class="pollLst clearfix">';
code += '<input type="radio" name="pollfid" value="0" id="rdoOpt5">';
code += '<label for="rdoOpt5" class="clTxt3 fs03">Other</label>';
code += '<input type="text" name="othername" class="clBg5 clBd2 fs03 clTxt3">';
code += '</div>';
code += '</div>';
}



code += '<div class="sendPoll">';
code += '<input type="button" onclick="pollfunction('+arraydata['productid']+','+arraydata['spollid']+')" value="VOTE" class="btnGen clTxtW cutTxt clBgSp2 fs04">';

code += '<input type="button" onclick="pollresultfunction('+arraydata['spollid']+',1)" value="Result" class="btnGen clTxtW cutTxt clBgSp2 fs04">';

code += '</div>';
code += '</div>';
code += '</div>';






code += '</div>';




code += '<div class="wrpHdInfoGen" id="pollvoteset2-'+arraydata['spollid']+'" style="display:none">';
code += '<h2 class="clTxt2 fs04 boldGen">Poll</h2>';
code += '<div class="wrpInFrmLd">';
code += '<h2 class="boldGen clTxt3 fs04">'+arraydata['title']+'</h2>';
code += '<div class="wrpPollInLd">';
code += '<div class="wrpPollOutLd">';








code += '</div>';

code += '<div class="sendPoll">';
code += '<input type="button" onclick="pollresultfunction('+arraydata['spollid']+',2)" value="Back To Vote" class="btnGen clTxtW cutTxt clBgSp2 fs04">';
code += '</div>';
code += '</div>';
code += '</div>';
code += '</div>';



code+='</div>';

if(roleidinfo>=parseInt(arraydata['userpoll'],10))
{
return code;

}else
{
code='';
return code;

}



}
function pollresultfunction(spollid,status)
{


if(status==1)
{
var parem=new Array(); 
parem[0]=spollid;
$.post(serviceinfo+"postmedthodplugin",{classset:'poll',medthodset:"pollresultfunction",parem:parem},
function(data){
var code='';
if(data.data.length)
{
$.each(data.data, function(key, value) {

code += '<div class="itFrmLd radioPollLd clearfix">';

code += '<div class="pollLst clearfix">';
code += '<span for="rdoOpt4" class="qustPoll clTxt3 fs03">'+value.title+'</span>';
code += '<span class="amtPoll clTxt3 fs03">'+value.countpoll+' Votes</span>';
code += '</div>';
code += '<div class="pcPoll clBd2 bdFull clearfix">';

code += '<span style="width:'+value.percent+'%" class="poll  clBgBt2"></span>';
code += '<span class="percPoll">'+value.percent+' %</span>';
code += '</div>';

code += '</div>';




})



}
$('#pollvoteset2-'+spollid+' .wrpPollOutLd').html(code);

$('#pollvoteset-'+spollid).fadeOut();
$('#pollvoteset2-'+spollid).fadeIn();

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {

}
else
{
$("#main-middle .wrpUinLd").mCustomScrollbar("update");

//setTimeout('rowscrolllanding();',1000);
}

});

}else
{
$('#pollvoteset-'+spollid).fadeIn();
$('#pollvoteset2-'+spollid).fadeOut();


var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {

}
else
{
$("#main-middle .wrpUinLd").mCustomScrollbar("update");

//setTimeout('rowscrolllanding();',1000);
}

}


}
function pollfunction(productid,spollid)
{
var parem=new Array();  
parem[0]=$('#pollfidset-'+spollid+' input[name="pollfid"]:checked').val();
parem[1]=productid;
parem[2]=spollid;
parem[3]=$('#pollfidset-'+spollid+' input[name="othername"]').val();

if(parem[0])
{


}else
{
boxmsgdata('Please Select Choice');
return false;
}


if(parem[0]=='0'&&parem[3]=='')
{

boxmsgdata('Please Input Other');
//$('#pollfidset-'+spollid+' input[name="othername"]').focus();
return false;

}

loaddata(); 
$.post(serviceinfo+"postmedthodplugin",{classset:'poll',medthodset:"pollfunction",parem:parem},
function(data){

if(data.error.response)
{
loadclosedata();   
if(data.error.error[0].code==5)
{
$('.ovlPopLd').hide();
$('.wrpPopLI').fadeIn();

//console.log('test');
}else
{
//alertbox(data.error.error);

alertbox(data.error.error);
}

}
else
{
loadclosedata();   
boxmsgdata('Done');

}


});

}
function deletechoicebtn(rownowset)
{

             var pollfid= $('#choiQust-'+rownowset).find('input[name="pollfid"]').val();
             var rowdatanow = $('#choiQust-'+rownowset).find('input[name="rowsetnow"]').val();
             if(pollfid!=''&&pollfid)
             {
             $('#feature'+rowdatanow).append('<input type="hidden" name="deletepollfid" value="'+pollfid+'">');
             
             }
             
              
              
              
              $('#choiQust-'+rownowset).remove();

}

function editformpoll(datarow)
{
       addformpoll();

       var rownow= rowdiv-1;
  //     var rowgnow2=1;
       $('#feature'+rownow+' input[name="questitle"]').val(datarow.title);
       
       var $radios = $('#feature'+rownow+' input[name="userpoll"]');
   // if($radios.is(':checked') === false) {
        $radios.filter('[value='+datarow.userpoll+']').prop('checked', true);
        
        
        $('#feature'+rownow+' input[name="spollid"]').val(datarow.spollid);
        
        
        if(datarow.form.length)
        {
        $.each(datarow.form, function(key, value) {

       //console.log( index + ": " + $( this ).text() );
       
        // $('#feature'+rownow+' input[name="spollid"]')
         
         addrowdata(rownow,value.pollfid,value.value);
       
       });
        
        }
        
        if(datarow.other)
        {
        $('#feature'+rownow+' input[name="otherpoll"]').filter('[value='+datarow.other+']').prop('checked', true);
        
        }
 //   }
       
      // $('#feature'+rownow+' input[name="questitle"]').val(datarow.title);
       //console.log(datarow.title);
       
       



}